<html>
    <head>
    </head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <body>
        <div>
            <input id="message" type="text"><button onclick="sendMessage()">发送</button>
        </div>

        <div>
            <div>服务器返回内容:</div>
            <div><span id="show"></span></div>
            
        </div>
    </body>
</html>


<script>

// var ws = new WebSocket("ws://127.0.0.1:55888/im?access_token=2253b8f2-3aa3-46ee-b1cf-a3a57129");
var ws = new WebSocket("ws://127.0.0.1:8080/websocket-service/im?access_token=0e449ceb-19d0-4ac5-9faf-aa6d993e9886");

var msesage = {};
msesage.username = "aa";
msesage.id = "6da3eb4299294a7280896fb3d6476f1d";
msesage.type = "0";
msesage.content = "d测试";
msesage.cid = "123";
msesage.fromid = "428edf4fcf0242a49198d32845b0b1ec";
var messJson = JSON.stringify(msesage);

var sendInfo = {};
sendInfo.message = msesage;
sendInfo.code = 2;
var sendInfoJson = JSON.stringify(sendInfo);


ws.onopen = function(evt) {
	console.log("Connection open ...");
    
};

 
ws.onmessage = function(evt) {
	console.log("Received Message: " + evt.data);
    alert(evt.data);
    $("#show").html(evt.data);
};
 
ws.onclose = function(evt) {
	console.log("Connection closed.");
	ws.close();
}


function sendMessage(){
    var status = ws.readyState;
    if(status == 1){
        var message = $("#message").val();
        ws.send(sendInfoJson);
        $("#message").attr("value","");
    }else{
        alert(status);
    }
   
}

$(window).unload(function(){
    ws.close();
});

</script>